package com.github.zxhTom.demo.jol;

/**
 * 指令重排序测试
 */
public class InstructionReorder {

    static volatile int a;
    static volatile int b;
    static volatile int x;
    static volatile int y;
    public static void main(String[] args) throws InterruptedException {
        int index=0;
        while (true) {
            index++;
            a=0;b=0;y=0;x=0;
            final Thread thread = new Thread(new Runnable() {
                public void run() {
                    a = 1;
                    x = b;
                }
            });

            final Thread thread1 = new Thread(new Runnable() {
                public void run() {
                    b = 1;
                    y = a;
                }
            });
            thread.start();thread1.start();
            thread.join();thread1.join();
            if (x == 0 && y == 0) {
                System.out.println("index="+index+"x,y="+x+y);
                break;
            }
        }
    }
}
